package com.ming.controller;

import com.ming.dao.CustomerMapper;
import com.ming.dao.PackageMapper;
import com.ming.dao.RecipientMapper;
import com.ming.dao.StaffMapper;
import com.ming.pojo.Customer;
import com.ming.pojo.Package;
import com.ming.pojo.Recipient;
import com.ming.pojo.Staff;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author Xu
 * Created by Intellij IDEA.
 * @create 2021/4/15 10:07
 */
@Controller
public class PackageController {
    @Autowired
    PackageMapper packageMapper;
    @Autowired
    StaffMapper staffMapper;
    @Autowired
    CustomerMapper customerMapper;
    @Autowired
    RecipientMapper recipientMapper;

    @RequestMapping("/packages")
    public String list(Model model){
        List<Package> packages = packageMapper.queryPackages();
        model.addAttribute("packages",packages);
        return "packages/list";
    }


    @RequestMapping("/staffPackages/{username}")
    public String staffList(@PathVariable("username") String username, Model model){
        //想办法得到这个sid
        Integer sid = staffMapper.queryStaffIdByName(username);
        List<Package> packages = packageMapper.queryStaffPackages(sid);
        model.addAttribute("packages",packages);
        return "packages/staffPList";
    }
    @RequestMapping("/userOPackages/{username}")
    public String userOList(@PathVariable("username") String username, Model model){
        //想办法得到这个cid,或者rid
        Integer cid = customerMapper.queryCustomerByName(username);
        if (cid != null){
            List<Package> packages = packageMapper.queryCustomerPackages(cid);
            model.addAttribute("packages",packages);
        }
        return "packages/userPList";
    }
    @RequestMapping("/userPackages/{username}")
    public String userList(@PathVariable("username") String username, Model model){
        //想办法得到这个cid,或者rid
        Integer rid = recipientMapper.queryRecipientByName(username);
        if (rid != null){
            List<Package> packages = packageMapper.queryRecipientPackages(rid);
            model.addAttribute("packages",packages);
        }
        return "packages/userPList";
    }

    @GetMapping("/package")
    public String toAddPage(Model model){
        //查出所有部门信息
        List<Package> packages = packageMapper.queryPackages();
        model.addAttribute("packages",packages);
        return "packages/add";
    }

    @GetMapping("/addp")
    public String toAddPage1(Model model){
        //查出所有部门信息
        List<Package> packages = packageMapper.queryPackages();
        model.addAttribute("packages",packages);
        return "packages/addPackage";
    }

    @PostMapping("/addPackage/{username}")
    public String addPackage1(@PathVariable("username") String username, Package _package){
        Integer mId = packageMapper.getMId();
        Integer cid = customerMapper.queryCustomerByName(username);
        packageMapper.addPackage(new Package(mId,_package.getPname(),_package.getType(),_package.getWeight(),
                _package.getNumber(),_package.getDelivery_time(),_package.getReturn_OrNot(),cid,60000,_package.getRid()));
        return "redirect:/main2.html";
    }

    @PostMapping("/package")
    public String addPackage(Package _package){
        _package.setPid(packageMapper.getMId());
        packageMapper.addPackage(_package);
        return "redirect:/packages";
    }



    //去客户的修改页面
    @GetMapping("/package/{id}")
    public String toUpdatePackage(@PathVariable("id") int id, Model model) {
        //查出原来的数据
        Package _package = packageMapper.queryPackageById(id);
        model.addAttribute("_package", _package);
        return "packages/update";
    }

    @PostMapping("/updatePackage")
    public String updatePackage(Package _package) {

        packageMapper.updatePackage(_package);
        return "redirect:/packages";
    }


    @GetMapping("/deletePackage/{id}")
    public String deletePackage(@PathVariable("id")Integer id){
        packageMapper.deletePackage(id);
        return "redirect:/packages";
    }

}
